package com.toutiao.vssue

import scala.concurrent.duration._

import org.apache.log4j.Logger

object PromiseDone {
  
  private val log = Logger.getLogger(this.getClass.getSimpleName)
  
  def promiseDone[A](body: => A, sleepTime: FiniteDuration = 2.seconds): A = {
    var done = false
    var result: Option[A] = None
    while (!done) {
      try {
        result = Some(body)
        done = true
      } catch {
        case t: Throwable =>
          log.error(s"promiseDone something wrong:" + "\n" +
            t + "\n" + t.getStackTrace.mkString("\n"))
          Thread.sleep(sleepTime.toMillis)
      }
    }
    result.get
  }  
  
}